﻿using Bomber.TF.GameCenter;
using Bomber.TF.View.ViewEx;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.AxHost;

namespace Bomber.TF.View
{
    public class PlayMap : Control
    {
        public const int ROWS = 15;
        public const int COLUMNS = 23;
        private readonly GameControlCenter _gameCenter;
        private readonly System.Timers.Timer _timer;
        private readonly Stopwatch _stopwatch;
        public PlayMap()
        {
            SetStyle(
           ControlStyles.ResizeRedraw
           | ControlStyles.DoubleBuffer
           | ControlStyles.UserPaint
           | ControlStyles.AllPaintingInWmPaint
           | ControlStyles.SupportsTransparentBackColor,
           true
           );
            _stopwatch= new Stopwatch();
            _gameCenter = new GameControlCenter(ROWS, COLUMNS, _cellWidthDefault, _cellHeightDefault);
            _timer = new System.Timers.Timer();
            _timer.Interval = 20;
            _timer.Elapsed += _timer_Elapsed;
        }
        public void Start()
        {
            _timer.Start();
            _stopwatch.Restart();
        }
        public void Stop() 
        {
            _timer.Stop();
            _stopwatch.Stop();
        }
        bool _isRun = false;
        private void _timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
#warning 调试用
            //if (_isRun) return;
            //lock (this)
            //{
            //    _isRun= true;
            //    _gameCenter.Business();
            //    this.Invalidate();
            //    _isRun = false;
            //}

            _gameCenter.Business();
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Stopwatch sw= Stopwatch.StartNew();
            Graphics g = e.Graphics;
            g.Smooth();


            _gameCenter.Draw(g);
            //DrawGrid(g);

            // 绘制时间
            long time = _stopwatch.ElapsedMilliseconds / 1000;
            string timeStr ="Time "+ time.ToString();
            SizeF sf = timeStr.MeasureString(this.Font);
            float x = (this.Width - sf.Width) / 2f;
            float y = _startY + 5 * _zoomY;
            g.DrawString(timeStr, this.Font, Brushes.Black, x, y);
            g.Smooth(false);
            sw.Stop();
            Trace.WriteLine($"UI耗时：{sw.ElapsedMilliseconds}ms");
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Up)
            {
                _gameCenter.ProcessCmdKeyUp();
                return true; // 返回true表示已处理该键盘输入
            }
            else if (keyData == Keys.Down)
            {
                _gameCenter.ProcessCmdKeyDown();
                return true;
            }
            else if (keyData == Keys.Left)
            {
                _gameCenter.ProcessCmdKeyLeft();
                return true;
            }
            else if (keyData == Keys.Right)
            {
                _gameCenter.ProcessCmdKeyRight();
                return true;
            }
            else if (keyData == Keys.Space)
            {
                _gameCenter.ProcessCmdKeySpace();
                return true;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
        
        private void DrawGrid(Graphics g)
        {
            using Pen pen = new Pen(Color.Red);
            int cellWidth = _cellWidth;
            int cellHeight = _cellHeight;
            int rows = ROWS;
            int cols = COLUMNS;
            int startX = _startX;
            int startY = _startY;
            for (int i = 0; i <= rows; i++)
            {
                g.DrawLine(pen, startX, startY + i * cellHeight, startX + cols * cellWidth, startY + i * cellHeight);
            }

            for (int i = 0; i <= cols; i++)
            {
                g.DrawLine(pen, startX + i * cellWidth, startY, startX + i * cellWidth, startY + rows * cellHeight);
            }
        }
        const int defaultW = 1200;
        const int defaultH = 800;
        const int _cellWidthDefault = 50;
        const int _cellHeightDefault = 50;

        private int _startX = 0;
        private int _startY = 0;
        private int _cellWidth = _cellWidthDefault;
        private int _cellHeight = _cellHeightDefault;
        private float _zoomX = 1f;
        private float _zoomY = 1f;
        protected override void OnSizeChanged(EventArgs e)
        {
            _zoomX = (float)this.Width / defaultW;
            _zoomY = (float)this.Height / defaultH;
            _cellWidth = (int)(_cellWidthDefault * _zoomX);
            _cellHeight = (int)(_cellHeightDefault * _zoomY);
            _startX = this.Width - _cellWidth * COLUMNS >> 1;
            _startY = this.Height - _cellHeight * ROWS >> 1;
            _gameCenter.SizeChange(ROWS, COLUMNS, _startX, _startY, _cellWidth, _cellHeight, _zoomX, _zoomY);

            this.Refresh();
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _gameCenter.Dispose();
        }
    }
}
